home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / shoot / athrust.lha / AmigaThrust / helpers / txt2c.c < prev   
Encoding:
C/C++ Source or Header  |  1998-09-05  |  2.7 KB  |  179 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #ifdef HAVE_CONFIG_H
  5. #include "../src/config.h"
  6. #endif
  7. #ifdef HAVE_UNISTD_H
  8. #include <unistd.h>
  9. #endif
  10. #ifndef AMIGA
  11. #include <sys/types.h>
  12. #endif
  13. #include <stdio.h>
  14.  
  15. #define BUFSIZE (2050)
  16.  
  17. void quit(char *str);
  18. int writebuf(int flag);
  19.  
  20. char buf[BUFSIZE];
  21.  
  22. void
  23. quit(char *str)
  24. {
  25.   perror(str);
  26.   exit(1);
  27. }
  28.  
  29. int
  30. writebuf(int flag)
  31. {
  32.   int stat,len,i;
  33.   char out[3];
  34.   
  35.   if(!flag) {
  36. #if PRINTF_RETURN >= 0
  37.     if(
  38. #endif
  39.        printf("\"")
  40. #if PRINTF_RETURN == 1
  41.                     != 1
  42. #endif
  43. #if PRINTF_RETURN >= 0
  44.                         )
  45.       return(1)
  46. #endif
  47.     ;
  48.  
  49.     len=strlen(buf);
  50.     if(len)
  51.       if(buf[len-1]=='\n')
  52.     buf[--len]=0;
  53.     for(i=0; i<len; i++) {
  54.       switch(buf[i]) {
  55.       case '\t':
  56.     strcpy(out,"\\t");
  57.     break;
  58.       case '\b':
  59.     strcpy(out,"\\b");
  60.     break;
  61.       case '\f':
  62.     strcpy(out,"\\f");
  63.     break;
  64.       case '\'':
  65.     strcpy(out,"\\\'");
  66.     break;
  67.       case '\"':
  68.     strcpy(out,"\\\"");
  69.     break;
  70.       case '\\':
  71.     strcpy(out,"\\\\");
  72.     break;
  73.       default:
  74.     out[0]=buf[i];
  75.     out[1]=0;
  76.       }
  77.       stat=fputs(out,stdout);
  78.       if(stat==EOF)
  79.     return(1);
  80.       if(stat<0)
  81.     return(1);
  82.     }
  83. #if PRINTF_RETURN >= 0
  84.     if(
  85. #endif
  86.        printf("\"")
  87. #if PRINTF_RETURN == 1
  88.                     != 1
  89. #endif
  90. #if PRINTF_RETURN >= 0
  91.                         )
  92.       return(1)
  93. #endif
  94.     ;
  95.   }
  96.   else {
  97. #if PRINTF_RETURN >= 0
  98.     if(
  99. #endif
  100.        printf("NULL")
  101. #if PRINTF_RETURN == 1
  102.                       != 4
  103. #endif
  104. #if PRINTF_RETURN >= 0
  105.                           )
  106.       return(1)
  107. #endif
  108.     ;
  109.   }
  110.  
  111.   return(0);
  112. }
  113.  
  114. int main(int argc, char *argv[])
  115. {
  116.   char *stat;
  117.   int end=0;
  118.  
  119.   if(argc!=2) {
  120.     fprintf(stderr, "%s: Usage '%s variable_name'\n",
  121.         argv[0],
  122.         argv[0]);
  123.     exit(1);
  124.   }
  125. #if PRINTF_RETURN >= 0
  126.   if(
  127. #endif
  128.      printf("\n#include <stddef.h>\n\nchar *%s[] = {\n", argv[1])
  129. #if PRINTF_RETURN == 1
  130.                                                    != (35 + strlen(argv[1]))
  131. #endif
  132. #if PRINTF_RETURN >= 0
  133.                                                                             )
  134.     quit(argv[0])
  135. #endif
  136.       ;
  137.  
  138.   while(!end) {
  139.     stat=fgets(buf, BUFSIZE, stdin);
  140.     if(stat==NULL) {
  141.       if(ferror(stdin))
  142.     quit(argv[0]);
  143.       end=1;
  144.       if(writebuf(1))
  145.     quit(argv[0]);
  146.     }
  147.     else {
  148.       if(writebuf(0))
  149.     quit(argv[0]);
  150. #if PRINTF_RETURN >= 0
  151.       if(
  152. #endif
  153.          printf(",\n")
  154. #if PRINTF_RETURN == 1
  155.                        != 2
  156. #endif
  157. #if PRINTF_RETURN >= 0
  158.                            )
  159.     quit(argv[0])
  160. #endif
  161.       ;
  162.     }
  163.   }  
  164. #if PRINTF_RETURN >= 0
  165.   if(
  166. #endif
  167.      printf(" };\n")
  168. #if PRINTF_RETURN == 1
  169.                      != 4
  170. #endif
  171. #if PRINTF_RETURN >= 0
  172.                          )
  173.     quit(argv[0])
  174. #endif
  175.       ;
  176.  
  177.   return(0);
  178. }
  179.